package com.liaoct.lct.db;

import java.io.ByteArrayOutputStream;

import com.liaoct.lct.activity.R;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;

/**
 * 数据库连接建立帮助类
 * 
 * @author liaoct
 * 
 */
public class DataBaseOpenHelper extends SQLiteOpenHelper {

	private Context context;

	public DataBaseOpenHelper(Context context, String name, int version) {
		super(context, name, null, version);
		this.context = context;
	}

	/**
	 * 第一次创建或打开数据库
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		StringBuffer sql = new StringBuffer();
		sql.append("  CREATE TABLE IF NOT EXISTS phone_type_info ( ");
		sql.append("  id   INTEGER        PRIMARY KEY ASC AUTOINCREMENT ");
		sql.append("  NOT NULL ");
		sql.append("  UNIQUE, ");
		sql.append("  name VARCHAR( 32 )   ");
		sql.append("  )");
		StringBuffer sql2 = new StringBuffer();
		sql2.append("  CREATE TABLE IF NOT EXISTS contacts_phone_info ( ");
		sql2.append("  id   INTEGER        PRIMARY KEY ASC AUTOINCREMENT ");
		sql2.append("  NOT NULL ");
		sql2.append("  UNIQUE, ");
		sql2.append("  contacts_id   INTEGER NOT NULL,   ");
		sql2.append("  phone_number  INTEGER,   ");
		sql2.append("  phone_type_id INTEGER NOT NULL    ");
		sql2.append("  )");
		StringBuffer sql3 = new StringBuffer();
		sql3.append("  CREATE TABLE IF NOT EXISTS photo_info ( ");
		sql3.append("  id   INTEGER        PRIMARY KEY ASC AUTOINCREMENT ");
		sql3.append("  NOT NULL ");
		sql3.append("  UNIQUE, ");
		sql3.append("  data   BLOB,   height INTEGER,    width  INTEGER    ");
		sql3.append("  )");
		StringBuffer sql4 = new StringBuffer();
		sql4.append("  CREATE TABLE IF NOT EXISTS contacts_info ( ");
		sql4.append("  id   INTEGER        PRIMARY KEY ASC AUTOINCREMENT ");
		sql4.append("  NOT NULL ");
		sql4.append("  UNIQUE, ");
		sql4.append("  name VARCHAR( 32 ), ");
		sql4.append("  photo_id INTEGER,");
		sql4.append("  times_contacted INTEGER DEFAULT ( 0 ),");
		sql4.append("  last_time_contacted INTEGER,");
		sql4.append("  company VARCHAR( 64 ),");
		sql4.append("  email VARCHAR( 256 ),");
		sql4.append("  address VARCHAR( 256 ),");
		sql4.append("  web VARCHAR( 256 ),");
		sql4.append("  first_letter CHAR( 2 ), letter VARCHAR(64) ");
		sql4.append("  )");

		db.execSQL("DELETE FROM contacts_info,photo_info,contacts_phone_info,phone_type_info");
		db.execSQL(sql.toString());
		db.execSQL(sql2.toString());
		db.execSQL(sql3.toString());
		db.execSQL(sql4.toString());
		db.execSQL("INSERT INTO [phone_type_info] ([id], [name]) VALUES (1, '手机')");
		db.execSQL("INSERT INTO [phone_type_info] ([id], [name]) VALUES (2, '住宅电话')");
		db.execSQL("INSERT INTO [phone_type_info] ([id], [name]) VALUES (3, '公司电话')");
		db.execSQL("INSERT INTO [contacts_phone_info] ([id], [contacts_id], [phone_number], [phone_type_id]) VALUES (1, 1, 15651775791, 1)");
		db.execSQL("INSERT INTO [contacts_phone_info] ([id], [contacts_id], [phone_number], [phone_type_id]) VALUES (2, 1, 15212421, 2)");
		db.execSQL("INSERT INTO [contacts_phone_info] ([id], [contacts_id], [phone_number], [phone_type_id]) VALUES (3, 2, 4336346, 3)");
		db.execSQL("INSERT INTO [contacts_info] ([id], [name], [photo_id], [times_contacted], [last_time_contacted], [company], [email], [address], [web], [first_letter]) VALUES (1, 'liaote', null, 111111, 11111, 1111, 111, 111, 111, null,null)");
		db.execSQL("INSERT INTO [contacts_info] ([id], [name], [photo_id], [times_contacted], [last_time_contacted], [company], [email], [address], [web], [first_letter]) VALUES (2, 'chenzheng', null, 222, 2222, 222, 222, 222, 222, null,null)");
		db.execSQL("INSERT INTO [contacts_info] ([id], [name], [photo_id], [times_contacted], [last_time_contacted], [company], [email], [address], [web], [first_letter]) VALUES (3, '廖', null, 123, 123, 123, 12, 123, 123, null,null)");
		db.execSQL("INSERT INTO [contacts_info] ([id], [name], [photo_id], [times_contacted], [last_time_contacted], [company], [email], [address], [web], [first_letter]) VALUES (4, '陈', null, 124, 124, 14, null, null, null, null,null)");
		db.execSQL("INSERT INTO [contacts_info] ([id], [name], [photo_id], [times_contacted], [last_time_contacted], [company], [email], [address], [web], [first_letter]) VALUES (5, '特', null, 523, 23523, 232, 23, 33, 33, null,null)");
		Bitmap bt = BitmapFactory.decodeResource(this.context.getResources(),
				R.drawable.head_p);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bt.compress(Bitmap.CompressFormat.PNG, 100, baos);
		byte[] images = baos.toByteArray();

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.i("***onUpgrade***", "oldVersion:" + oldVersion + ",newVersion:"
				+ newVersion);
	}

}
